package day05;

import util.JdbcUtil;

import java.io.Serializable;
import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.ArrayList;
import java.util.List;

public class Cart implements Serializable {
    private List<Item> items=new ArrayList<>();
    private Double price;

    public Cart() {
        price=0.0;
    }

    public void add(Integer productId){
        Product product=findProductById(productId);

        //总价钱+当前商品的单价
        price+=product.getPrice();

        //判断当前商品在购物车中是否存在
        for (Item item : items) {
            if(item.getProduct().getId().equals(productId)) {
                //数量+1
                item.setNum(item.getNum()+1);
                //明细价格+当前商品的单价
                item.setPrice(item.getPrice()+product.getPrice());
                return;
            }

        }

        //购物车中不存在
        //创建一个Item对象
        Item item=new Item();
        item.setProduct(product);
        item.setNum(1);
        item.setPrice(product.getPrice());
        //将这个对象加到items中
        items.add(item);
    }

    public void removeByProductId(Integer productId){
        for (int i = 0; i < items.size(); i++) {
            Item item=items.get(i);
            if(item.getProduct().getId().equals(productId)){
                price-=item.getPrice();
                items.remove(item);
            }
        }
    }

    public void removeByProductIds(List<Integer> productIds){
        for (Integer productId : productIds) {
            removeByProductId(productId);
        }
    }

    public void clear(){
        items=new ArrayList<>();
        price=0.0;
    }

    public void modify(Integer productId,Integer num){
        Product product=findProductById(productId);

        for (Item item : items) {
            if(item.getProduct().getId().equals(productId)){
                price-=item.getPrice();
                item.setNum(num);
                item.setPrice(product.getPrice()*num);
                price+=item.getPrice();
            }
        }
    }

    private Product findProductById(Integer id){
        Product product=null;
        Connection con=null;
        PreparedStatement ps=null;
        ResultSet rs=null;
        try{
            con= JdbcUtil.getConnection();
            con.setAutoCommit(false);


            String sql=new StringBuffer()
                    .append("select * ")
                    .append("from t_product ")
                    .append("where id=? ")
                    .toString();

            ps=con.prepareStatement(sql);

            ps.setInt(1,id);

            rs=ps.executeQuery();

            if(rs.next()){
                product=new Product();
                product.setId(rs.getInt("id"));
                product.setName(rs.getString("name"));
                product.setPrice(rs.getDouble("price"));
            }

            con.commit();
        }catch (Exception e){
            e.printStackTrace();
            try {
                con.rollback();
            } catch (SQLException ex) {
                ex.printStackTrace();
            }
        }finally{
            JdbcUtil.close(rs,ps,con);
        }
        return product;
    }


    public List<Item> getItems() {
        return items;
    }

    public void setItems(List<Item> items) {
        this.items = items;
    }

    public Double getPrice() {
        return price;
    }

    public void setPrice(Double price) {
        this.price = price;
    }
}
